package com.itheima.health.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = MenuService.class)
@Slf4j
public  class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;

    /**
     * 查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> page = menuDao.findAll(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增
     * @param menu
     */
    @Override
    public void menuAdd(Menu menu) {
        menuDao.menuAdd(menu);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public Menu menuUpdate(Integer id)
    {
        return menuDao.menuUpdate(id);
    }

    /**
     * 更新
     * @param menu
     */
    @Override
    public void menuEdit(Menu menu) {
   menuDao.menuEdit(menu);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void menuDelete(Integer id)  {

        menuDao.menuDelete(id);
    }

    @Override
    public List<Menu> findByRoleId(int id) {
        return menuDao.findByRoleId(id);
    }

    /**
     * 动态获取菜单
     * @return
     */
    @Override
    public List<Menu> getMenu(String username) {

        List<Menu> menuList=new ArrayList<>();
        User user = userDao.selectByUsername(username);

        //查询所有菜单id
        List<Menu> menus=menuDao.getMenu(user.getId());

        //循环所有菜单
        for (int i = 0; i < menus.size(); i++) {
            //获取所有一级菜单
            if((menus.get(i).getParentMenuId())==null){
                menuList.add(menus.get(i));
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (Menu menu : menuList) {
            menu.setChildren(getChild(menu.getId(),menus));
        }
        return menuList;
    }

    @Override
    public List<Menu> menuName() {
        return menuDao.menuName();
    }

    @Override
    public List<Menu> findRoleMenus() {

        return menuDao.findRoleMenus();
    }
//
    private  List<Menu> getChild(Integer id,List<Menu> rootMenu){
        // 子菜单
        List<Menu> childList = new ArrayList<>();
        for (Menu menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (menu.getParentMenuId()!=null) {
                if (menu.getParentMenuId() == id) {
                    childList.add(menu);
                }
            }
        }
        // 把父菜单的子菜单再循环一遍
        for (Menu menu : childList) {// 没有url子菜单还有子菜单
            if (StringUtils.isEmpty(menu.getLinkUrl())) {
                // 递归
                menu.setChildren(getChild(menu.getId(), rootMenu));
            }
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

}